<?php

namespace app\webapi\common;

use Godruoyi\Snowflake\Snowflake;
use think\Model;

class BaseModel extends Model
{
    /**
     * @desc 重写model-save方法，添加雪花算法id
     * @param array $data
     * @param string|null $sequence
     * @return bool
     * @author zhaozhiwei
     * @time 2023/12/14-9:18
     */
    public function save(array $data = [], string $sequence = null): bool
    {
        $this->setSnowId();
        return parent::save($data, $sequence);
    }

    /**
     * @desc 重写model-insert方法，添加雪花算法id
     * @param array $data
     * @param string|null $sequence
     * @return bool
     * @author zhaozhiwei
     * @time 2023/12/14-9:18
     */
    public function insert(array $data = [], bool $getLastInsID = false)
    {
        $this->setSnowId();
        return parent::insert($data, $getLastInsID);
    }

    /**
     * @desc 设置snowId
     * @return void
     * @author zhaozhiwei
     * @time 2023/12/14-9:19
     */
    protected function setSnowId()
    {
        $this->setAttr('id', $this->getSnowId());
    }

    /**
     * @desc 获取snowId
     * @return void
     * @author zhaozhiwei
     * @time 2023/12/14-9:19
     */
    protected function getSnowId()
    {
        $snowflake = new Snowflake(100, 100);
        return $snowflake->id();
    }
}